<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 设置客服 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div style="display:flex; justify-content:space-between">
                <div>
                    <el-button type="primary" class="save" @click="add()">添加客服</el-button>
                    <el-button type="primary" class="save" @click="update()">清空聊天记录</el-button>
                </div>
                <div style="    line-height: 55px;" v-if="unread_message">
                    <span style="color: red;margin-top:5px"><i class="el-icon-info"></i>当前{{unread_message}}条消息未读</span>
                </div>
            </div>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column label="客服头像" width="100">
                    <template #default="{ row }">
                        <!-- <img :src="row.kefu_avatar" class="avatarimg" alt="" /> -->
                        <img :src="row.kefu_avatar" @error="tabErrorPicture(row.id)" alt="" class="avatarimg" v-if="row.kefu_avatar" />
                        <img :src="errorProduct" alt="" class="avatarimg" v-else />
                    </template>
                </el-table-column>
                <el-table-column prop="kefu_nickname" label="客服名称"></el-table-column>
                <el-table-column label="操作" width="300" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="success" @click="onchat(scope.row.id)">进入聊天</el-button>
                        <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
                        <el-button type="danger" @click="del(scope.row.id, scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog :visible.sync="dialogFormVisible" width="540px">
            <el-form label-width="120px" :model="form">
                <el-form-item label="头像">
                    <div class="avatar-uploader" @click="showUploadImage()">
                        <img v-if="form.kefu_avatar" :src="form.kefu_avatar" class="avatar" height="200px" />
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </div>
                    <div class="el-upload__tip">封面建议尺寸：600*600px，支持jpg、png</div>
                </el-form-item>
                <el-form-item label="客服昵称">
                    <el-input v-model="form.kefu_nickname" type="text" />
                </el-form-item>
                <el-form-item label="客服登录用户名">
                    <el-input v-model="form.kefu_username" type="text" />
                </el-form-item>
                <el-form-item label="客服登录密码">
                    <el-input v-model="form.kefu_password" type="text" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button
                    @click="
                        dialogFormVisible = false;
                        form = {};
                    "
                    >取 消</el-button
                >
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
        <uploadimage ref="uploadimagecom" v-on:childByImageValue="childByImageValue"></uploadimage>
    </div>
</template>
<script>
import { addKeFu, getKeFu, delKeFu } from '../../api/index';
import uploadimage from '@/components/common/uploadimage.vue';
export default {
    components: {
        uploadimage
    },
    data() {
        return {
            tableData: [],
            dialogFormVisible: false,
            nickname: '',
            username: '',
            password: '',
            dialogVisible: false,
            form: '',
            unread_message:"",
            errorProduct: require('@/assets/img/avatar.png'), 
        };
    },
    async created() {
        this.getData();
    },
    methods: {
        tabErrorPicture(id){    // 表格图文封面默认展示图
            console.log('error',id)
            let i = this.tableData.findIndex(v => v.id == id);
			this.tableData[i].kefu_avatar = this.errorProduct;
        },
        async getData() {
            let res = await getKeFu();
            if (res.code == 1) {
                this.tableData = res.data.list;
                this.unread_message=res.data.unread_message
                console.log(res)
            }
        },
        add() {
            this.form = {
                kefu_avatar: '',
                kefu_nickname: '',
                kefu_username: '',
                kefu_password: ''
            };
            this.dialogFormVisible = true;
        },
        edit(row) {
            this.form = row;
            this.dialogFormVisible = true;
        },
        async save() {
            let res = await addKeFu(this.form);
            if (res.code == 1) {
                this.$alert('客服数据保存成功', '消息提示', { confirmButtonText: '确定' });
                this.dialogFormVisible = false;
                this.form = {};
                this.getData();
            }
        },
        showUploadImage() {
            //显示上传组件
            this.$refs.uploadimagecom.dialogVisible = true;
        },
        childByImageValue(childValue) {
            this.form.kefu_avatar = childValue;
        },
        update() {
            this.$confirm('此举动将只保留最近15天的聊天记录，是否确定？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    this.$message({ type: 'success', message: '删除客服成功' });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        del(id, index) {
            console.log(id);
            this.$confirm('此操作将永久删除该客服, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    let res = await delKeFu({ id });
                    if (res.code == 1) {
                        this.tableData.splice(index, 1);
                        this.$message({ type: 'success', message: '删除客服成功' });
                    } else {
                        this.$message({ type: 'error', message: '删除客服失败' });
                    }
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        onchat(id) {
            this.$router.push({ path: '/infoManage/reply_msg', query: { id } });
        }
    }
};
</script>
<style lang='scss' scoped>
.avatarimg {
    width: 50px;
    height: 50px;
}

.save {
    margin: 10px;
}

.avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 375px !important;
    text-align: center;
}

.avatar-uploader:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
}

.avatar {
    width: 100%;
    display: block;
}
</style>
